package com.xiaoyilin.demo.config;

import com.google.common.collect.Sets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class Swagger2Config implements WebMvcConfigurer {

    /**
     * 接口文档默认访问路径
     * http://localhost:8080/swagger-ui.html
     * http://localhost:8080/doc.html
     */
    @Bean
    public Docket commonDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("API接口文档")
                .apiInfo(apiInfo("开发环境通用接口"))
                .protocols(Sets.newHashSet("https", "http"))
                // .produces(Sets.newHashSet("html/text"))
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xiaoyilin.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo(String desc) {
        return new ApiInfoBuilder()
                .title(desc)
                .contact(new Contact("eLin", "http://demo.xiaoyilin.com/api", "406735078@qq.com"))
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0")
                .description("API 描述")
                .build();
    }


}
